﻿#if WINDOWS_PHONE
#else
using Microsoft.Xna.Framework.Net;

namespace JGLib.Net
{
    internal static class Util
    {
        public static void InitSession(this NetworkSession session, NetUtil.GamerJoinedEvent gamerJoined, NetUtil.GamerLeftEvent gamerLeft,
            NetUtil.SessionJoinedEvent sessionJoined)
        {
            session.GamerJoined += ((sender, e) =>
            {
                if (gamerJoined != null)
                    gamerJoined(e);
            });
            session.GamerLeft += ((sender, e) =>
            {
                session.Dispose();
                session = null;
                if (gamerLeft != null)
                    gamerLeft(e);
            });
            if (sessionJoined != null)
                sessionJoined(session);
        }

        public static void ProcessIncomingData(this LocalNetworkGamer localGamer, PacketReader reader, NetUtil.ReadPackets read)
        {
            while (localGamer.IsDataAvailable)
            {
                NetworkGamer sender;
                localGamer.ReceiveData(reader, out sender);

                if (!sender.IsLocal && read != null)
                    read(reader);
            }
        }
    }

    public static class NetUtil
    {
        public delegate void GamerJoinedEvent(GamerJoinedEventArgs e);
        public delegate void GamerLeftEvent(GamerLeftEventArgs e);
        public delegate void SessionJoinedEvent(NetworkSession session);

        public delegate void ReadPackets(PacketReader reader);
    }
}
#endif
